package cn.edu.scau.cmi.ema.service;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Crawlerknowledge;
import cn.edu.scau.cmi.ema.domain.Url;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.*;
@Service("CrawlerknowledgeService")
@Transactional

public class CrawlerknowledgeServiceImpl extends CmiEmaService implements CrawlerknowledgeService {
	public CrawlerknowledgeServiceImpl() {
	}

	@Transactional
	public Crawlerknowledge saveCrawlerknowledge(Crawlerknowledge crawlerknowledge) {

		if (crawlerknowledge.getId() == null) {
			Url url = urlDAO.findUrlById(crawlerknowledge.getUrl().getId());

			Crawlerknowledge existingCrawlerknowledge = crawlerknowledgeDAO
					.findCrawlerknowledgeByPhysicalPrimaryKey(url.getId());

			crawlerknowledge.setUrl(url);

			if (isEquals(crawlerknowledge, existingCrawlerknowledge)) {
				return existingCrawlerknowledge;
			}

			crawlerknowledge = crawlerknowledgeDAO.store(crawlerknowledge);
			crawlerknowledgeDAO.flush();

		} else {
			Crawlerknowledge existingCrawlerknowledge = crawlerknowledgeDAO
					.findCrawlerknowledgeByPrimaryKey(crawlerknowledge.getId());

			crawlerknowledge = crawlerknowledgeDAO.store(crawlerknowledge);
			crawlerknowledgeDAO.flush();
		}

		return crawlerknowledge;
	}

	public void deleteCrawlerknowledge(Crawlerknowledge crawlerknowledge) {
		crawlerknowledgeDAO.remove(crawlerknowledge);
		crawlerknowledgeDAO.flush();
	}

	@Transactional
	public void deleteCrawlerknowledges(Set<Integer> deleteCrawlerknowledgesId) {
		Query query = crawlerknowledgeDAO.createNamedQuery("deleteCrawlerknowledgesById", -1, -1);
		query.setParameter(1, deleteCrawlerknowledgesId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Crawlerknowledge> loadCrawlerknowledges() {
		return crawlerknowledgeDAO.findAllCrawlerknowledges();

	}

	@Transactional
	public Set<Crawlerknowledge> loadReferenceCrawlerknowledges(Set<Crawlerknowledge> crawlerknowledges) {
		Iterator<Crawlerknowledge> crawlerknowledgesIterator = crawlerknowledges.iterator();
		Set<Crawlerknowledge> referencedCrawlerknowledges = new HashSet<>();
		while (crawlerknowledgesIterator.hasNext()) {
			Crawlerknowledge currentCrawlerknowledge = crawlerknowledgesIterator.next();

		}
		return referencedCrawlerknowledges;
	}

	public Set<Crawlerknowledge> loadCrawlerknowledgesByIdSet(Set<Integer> crawlerknowledgeIdSet) {

		Iterator<Integer> crawlerknowledgeIdIterator = crawlerknowledgeIdSet.iterator();
		Set<Crawlerknowledge> crawlerknowledgeSet = new HashSet<>();
		while (crawlerknowledgeIdIterator.hasNext()) {
			Integer id = crawlerknowledgeIdIterator.next();
			Crawlerknowledge currentCrawlerknowledge = crawlerknowledgeDAO.findCrawlerknowledgeById(id);
			crawlerknowledgeSet.add(currentCrawlerknowledge);
		}
		return crawlerknowledgeSet;
	}

	@Transactional
	public List<Crawlerknowledge> findAllCrawlerknowledges(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Crawlerknowledge>(
				crawlerknowledgeDAO.findAllCrawlerknowledges(startResult, maxRows));
	}

	@Transactional
	public Crawlerknowledge findCrawlerknowledgeByPrimaryKey(Integer id) {
		return crawlerknowledgeDAO.findCrawlerknowledgeByPrimaryKey(id);
	}

	@Transactional
	public Integer countCrawlerknowledges() {
		return ((Long) crawlerknowledgeDAO.createQuerySingleResult("select count(o) from Crawlerknowledge o")
				.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countCrawlerknowledges(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Crawlerknowledge.class);
		return ((Long) crawlerknowledgeDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 *
	 */
	@Transactional
	public Integer countCrawlerknowledgesByName(String queryName) {
		Query query = crawlerknowledgeDAO.createNamedQuery("countCrawlerknowledgesByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countCrawlerknowledgesByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerknowledge.class);
		Session session = (Session) entityManager.getDelegate();
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadCrawlerknowledgeObjectReferencedSet(Crawlerknowledge crawlerknowledge) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();
		if (crawlerknowledge.getUrl() != null) {

			Set<Integer> urlIds = new HashSet<Integer>();
			urlIds.add(crawlerknowledge.getUrl().getId());
			objectReferencedSetMap.put("urlIds", urlIds);
		}

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadCrawlerknowledgeClassReferencedSet(Crawlerknowledge crawlerknowledge) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();
		classReferencedSetMap.put("urls", urlDAO.findAllUrls());

		return classReferencedSetMap;
	}

	public boolean isEquals(Crawlerknowledge crawlerknowledge, Object obj) {
		if (obj == crawlerknowledge)
			return true;
		if (!(obj instanceof Crawlerknowledge))
			return false;
		Crawlerknowledge comparedCrawlerknowledge = (Crawlerknowledge) obj;
		if (!CheckPropertyEqual(crawlerknowledge.getUrl().getId(), comparedCrawlerknowledge.getUrl().getId())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

